<!DOCTYPE html>
<html>

  <head>
    <meta charset="UTF-8">
    <title></title>
  </head>

  <body>
    <script type="text/javascript">
      var year = prompt("请输入年份","2018")*1;
      var month = prompt("请输入月份","7")*1;
      var day = 0;
      var week1 = 0;
      var sumday = 0;
      //计算年总天数
      for (var n = 1900;n < year; n++) {
      	if(n % 4 == 0 && n % 100 != 0 || n % 400 == 0) {
            sumday+=366;
          } else {
            sumday+=365;
          }
      }
      
      //计算月总天数和
      for(y = 1;y < month;y++){
        if (y == 2) {
        	if(n % 4 == 0 && n % 100 != 0 || n % 400 == 0) {
            sumday+=29;
          } else {
            sumday+=28;
          }
        }else if (y == 4 || y == 6 || y == 9 || y == 11) {
        	sumday+=30;
        }else{
          sumday+=31;
        }
      }
      week1 = (sumday + 1) % 7;
      //判断某年某月有多少天
      switch(month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
          day = 31;
          break;
        case 4:
        case 6:
        case 9:
        case 11:
          day = 30;
          break;
        case 2:
          if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            day = 29;
          } else {
            day = 28;
          }
          break;
      }
      document.write(year + "年" + month + "月" + "有" + day + "天<br/>");
      document.write("<table border='1' cellspacing='0' width='400'>");
      document.write("<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>");
      document.write("<tr>");
      //打印空的td
      for (var i = 1;i <= week1;i++) {
      	document.write("<td></td>");
      }
      for(var i = 1; i <= day; i++) {
        document.write("<td>" + i + "</td>");
        if((i + week1) % 7 == 0) {
          document.write("<tr></tr>");
        }
      }
      var endweek = (day + week1 - 1) % 7;
      //补全后面空的td
      for (var i = 0;i < 6 - endweek;i++) {
      	document.write("<td></td>");
      }
      document.write("</tr>");
      document.write("</table>");
    </script>
  </body>

</html>